package com.mgmaps.utils;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class LogForm extends Form implements CommandListener {
  public static final Command SHOW_LOG = new Command("Show log", Command.ITEM, 97);

  private Command back;
  private Command update;
  private Command clear;

  private final Displayable backTo;

  private final MIDlet midlet;

  public LogForm(final MIDlet midlet, final Displayable backTo) {
    super("Library log");
    this.midlet = midlet;
    this.backTo = backTo;
    commandAction(update, this);

    update = new Command("Update", Command.SCREEN, 3001);
    clear = new Command("Clear", Command.SCREEN, 3002);
    back = new Command("Back", Command.BACK, 3003);

    addCommand(update);
    addCommand(clear);
    addCommand(back);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == update) {
      deleteAll();
      append(ExtLogger.getLogString());
    }
    else if (cmd == clear) {
      ExtLogger.clear();
      commandAction(update, d);
    }
    else if (cmd == back) {
      Display.getDisplay(midlet).setCurrent(backTo);
    }
  }
}
